package Utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class StringUtil {

	public static String quotedStr(String valor) {
		return "'" + valor + "'";
	}

	public static Calendar stringToCalendar(String data) {
		Date d;
		try {
			d = new SimpleDateFormat("dd/MM/yyyy").parse(data);
			Calendar c = Calendar.getInstance();
			c.setTime(d);
			return c;
		} catch (ParseException ex) {
			return null;
		}
	}

	public static java.sql.Date calendarToSqlDate(Calendar data) {
		java.sql.Date sqlDate = new java.sql.Date(data.getTimeInMillis());
		return sqlDate;
	}

	public static String limpaTelefone(String telefone) {
		telefone = telefone.replace("(", "");
		telefone = telefone.replace(")", "");
		telefone = telefone.replace("-", "");
		return telefone;
	}

	public static String limpaCep(String cep) {
		cep = cep.replace("-", "");
		return cep;
	}

	public static String limpaData(String data) {
		data = data.replace("/", "");
		data = data.replace("-", "");
		return data;
	}

	public static java.sql.Date stringToSqlDate(String data) {
		DateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
		try {
			return new java.sql.Date(fmt.parse(data).getTime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static String calendarToString(Calendar calendario) {
		Date data = calendario.getTime();
		return formatarData(data, "dd/MM/yyyy");
	}

	public static String formatarData(Date data, String formato) {
		SimpleDateFormat sdf = new SimpleDateFormat(formato);
		return sdf.format(data);
	}
	
}
